﻿using System;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars;
using DevExpress.XtraEditors;
using DevExpress.XtraLayout.Utils;
using NNS.Framework;
using NNS.UI;
using QLTB.ThongKeThietBi.UI;

namespace QLTB.TimKiem.UI
{
    public partial class frmThongKeThietBi : XtraForm
    {
        private MyExports myExports;

        public frmThongKeThietBi()
        {
            InitializeComponent();
            Nap();
            dtden.DateTime = DateTime.Now;
            dttu.DateTime = DateTime.Now;
            if (!MyLogin.Original.toantinh)
            {
                liDonvi.Visibility = LayoutVisibility.Never;
                chxDonvi.Checked = true;
                txtDonvi.Enabled = false;
                txtDonvi.EditValue = MyLogin.Original.iddonvi;
            }
        }

        private frmMain MyRibbon
        {
            get { return ParentForm as frmMain; }
        }

        public MyExports MyExport
        {
            get
            {
                if (myExports == null)
                {
                    myExports = new MyExports();
                    myExports.ExportView = gbList;
                    myExports.ExportPercent += myExports_ExportPercent;
                    myExports.ExportComplete += myExports_ExportEnded;
                }
                return myExports;
            }
        }

        private void Nap()
        {
            try
            {
                tHIETBITableAdapter.Connection.ConnectionString = DbHelper.ConnectionString;
                tHIETBITableAdapter.Fill(dsThietBi.THIETBI);
            }
            catch (Exception ex)
            {
                MyBox.ShowWarning(ex.Message, "Tìm kiếm");
            }
        }

        private void myExports_ExportEnded(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
            MyRibbon.ShowProcess = BarItemVisibility.Never;
        }

        private void myExports_ExportPercent(object sender, ExportPercentEventArgs e)
        {
            MyRibbon.SetValue(e.Position);
        }

        protected override void OnParentChanged(EventArgs e)
        {
            base.OnParentChanged(e);
            if (MyRibbon != null)
            {
                MyRibbon.MyRibbon.SetPopupContextMenu(gcList, MyRibbon.pm);
            }
        }

        private void iPdf_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (!Visible) return;

            MyExport.ExportPdf();
        }

        private void iWord_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (!Visible) return;
            MyExport.ExportRtf();
        }

        private void iExcel_ItemClick(object sender, ItemClickEventArgs e)
        {
            //throw new NotImplementedException();
            if (!Visible) return;
            MyExport.Export();
        }

        private void iPreview_ItemClick(object sender, ItemClickEventArgs e)
        {
            //throw new NotImplementedException();
            if (!Visible) return;
            Preview();
        }

        private void Preview()
        {
            var rpt = new rptThongKeThietBi(dsThietBi.THIETBI);
            // rpt.DataSource = dsThietBi.THIETBI;
            // MessageBox.Show(dsThietBi.THIETBI.Rows.Count.ToString());
            rpt.ShowPreview();
        }

        private void iQuickPrint_ItemClick(object sender, ItemClickEventArgs e)
        {
            //throw new NotImplementedException();
            if (!Visible) return;
            Print();
        }

        private void Print()
        {
            var rpt = new rptThongKeThietBi(dsThietBi.THIETBI);
            // rpt.DataSource = dsThietBi.THIETBI;
            // MessageBox.Show(dsThietBi.THIETBI.Rows.Count.ToString());
            rpt.Print();
        }

        private void iPrint_ItemClick(object sender, ItemClickEventArgs e)
        {
            //throw new NotImplementedException();
            if (!Visible) return;
            PrintDialog();
        }

        private void PrintDialog()
        {
            var rpt = new rptThongKeThietBi(dsThietBi.THIETBI);
            // rpt.DataSource = dsThietBi.THIETBI;
            // MessageBox.Show(dsThietBi.THIETBI.Rows.Count.ToString());
            rpt.PrintDialog();
        }

        private void iXem_ItemClick(object sender, ItemClickEventArgs e)
        {
            //throw new NotImplementedException();
            if (!Visible) return;
            Go();
        }


        private void iSearch_ItemClick(object sender, ItemClickEventArgs e)
        {
            //throw new NotImplementedException();
            //if (!Visible) return;
            //if (condition==null)
            //{
            //    condition=new frmCondition();
            //}
            //if (!condition.Visible) condition.Show(this);
        }

        private void iClose_ItemClick(object sender, ItemClickEventArgs e)
        {
            //throw new NotImplementedException();
            if (!Visible) return;
            Close();
        }


        private void frmTimKiem_Activated(object sender, EventArgs e)
        {
            var f = (frmMain) ParentForm;
            if (f != null)
            {
                f.IgClose.Visible = true;
                f.iEdit.Visible = false;
                f.iUndo.Visibility = BarItemVisibility.Never;
                f.iCancel.Visibility = BarItemVisibility.Never;
                f.isPrint.Visibility = BarItemVisibility.Always;
                f.iSave.Visibility = BarItemVisibility.Never;
                f.iClose.Visibility = BarItemVisibility.Always;
                f.iRefesh.Visibility = BarItemVisibility.Never;
                // f.iNavigate.Visible = false;
                f.iComponents.Visibility = BarItemVisibility.Never;
                f.iEquipHistory.Visibility = BarItemVisibility.Never;
                f.iDelete.Visibility = BarItemVisibility.Never;
                f.iCopy.Visibility = BarItemVisibility.Never;
                f.iSelectAll.Visibility = BarItemVisibility.Never;
                f.iClear.Visibility = BarItemVisibility.Never;

                f.iXem.Visibility = BarItemVisibility.Always;
                f.iExport.Visibility = BarItemVisibility.Always;
                f.iWord.Visibility = BarItemVisibility.Always;
                f.iPdf.Visibility = BarItemVisibility.Always;
                f.iExcel.Visibility = BarItemVisibility.Always;
                f.igPrint.Visible = true;
                f.iPrint.Visibility = BarItemVisibility.Always;
                f.iQuickPrint.Visibility = BarItemVisibility.Always;
                f.iPreview.Visibility = BarItemVisibility.Always;


                MyRibbon.iClose.ItemClick += iClose_ItemClick;
                MyRibbon.iSearch.ItemClick += iSearch_ItemClick;
                MyRibbon.iXem.ItemClick += iXem_ItemClick;
                MyRibbon.iPrint.ItemClick += iPrint_ItemClick;
                MyRibbon.iQuickPrint.ItemClick += iQuickPrint_ItemClick;
                MyRibbon.iPreview.ItemClick += iPreview_ItemClick;
                MyRibbon.iExcel.ItemClick += iExcel_ItemClick;
                MyRibbon.iWord.ItemClick += iWord_ItemClick;
                MyRibbon.iPdf.ItemClick += iPdf_ItemClick;
            }
            NapInf();
        }

        private void frmTimKiem_Deactivate(object sender, EventArgs e)
        {
            var f = (frmMain) ParentForm;
            if (f != null)
            {
                f.IgClose.Visible = false;
                f.iEdit.Visible = false;
                f.iUndo.Visibility = BarItemVisibility.Never;
                f.iCancel.Visibility = BarItemVisibility.Never;
                f.isPrint.Visibility = BarItemVisibility.Never;
                f.iSave.Visibility = BarItemVisibility.Never;
                f.iClose.Visibility = BarItemVisibility.Never;
                f.iRefesh.Visibility = BarItemVisibility.Never;
                // f.iNavigate.Visible = false;
                f.iComponents.Visibility = BarItemVisibility.Never;
                f.iEquipHistory.Visibility = BarItemVisibility.Never;
                f.iDelete.Visibility = BarItemVisibility.Never;
                f.iCopy.Visibility = BarItemVisibility.Never;
                f.iSelectAll.Visibility = BarItemVisibility.Never;
                f.iClear.Visibility = BarItemVisibility.Never;

                f.iXem.Visibility = BarItemVisibility.Never;
                f.iExport.Visibility = BarItemVisibility.Never;
                f.iWord.Visibility = BarItemVisibility.Never;
                f.iPdf.Visibility = BarItemVisibility.Never;
                f.iExcel.Visibility = BarItemVisibility.Never;
                f.igPrint.Visible = false;
                f.iPrint.Visibility = BarItemVisibility.Never;
                f.iQuickPrint.Visibility = BarItemVisibility.Never;
                f.iPreview.Visibility = BarItemVisibility.Never;


                MyRibbon.iClose.ItemClick -= iClose_ItemClick;
                MyRibbon.iSearch.ItemClick -= iSearch_ItemClick;
                MyRibbon.iXem.ItemClick -= iXem_ItemClick;
                MyRibbon.iPrint.ItemClick -= iPrint_ItemClick;
                MyRibbon.iQuickPrint.ItemClick -= iQuickPrint_ItemClick;
                MyRibbon.iPreview.ItemClick -= iPreview_ItemClick;
                MyRibbon.iExcel.ItemClick -= iExcel_ItemClick;
                MyRibbon.iWord.ItemClick -= iWord_ItemClick;
                MyRibbon.iPdf.ItemClick -= iPdf_ItemClick;
            }
        }

        private void frmTimKiem_FormClosing(object sender, FormClosingEventArgs e)
        {
            Hide();
            //if (condition != null) condition.Close();
            e.Cancel = !Program._exit;
        }

        private void frmTimKiem_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'dsMucdich.TBL_Mucdich' table. You can move, or remove it, as needed.

            // TODO: This line of code loads data into the 'dsThietBi.THIETBI' table. You can move, or remove it, as needed.

            // TODO: This line of code loads data into the 'dsTinhTrang.TBL_Tinhtrang' table. You can move, or remove it, as needed.

            // TODO: This line of code loads data into the 'dsNHACUNGCAP.TBL_NHACUNGCAP' table. You can move, or remove it, as needed.
            NapInf();
        }

        private void NapInf()
        {
            try
            {
                tBL_TinhtrangTableAdapter.Connection.ConnectionString = DbHelper.ConnectionString;
                tBL_TinhtrangTableAdapter.Fill(dsTinhTrang.TBL_Tinhtrang);
                tBL_loaiTBTableAdapter.Connection.ConnectionString = DbHelper.ConnectionString;
                tBL_loaiTBTableAdapter.Fill(dsLoaiTB.TBL_loaiTB);
                tBL_DONVITableAdapter.Connection.ConnectionString = DbHelper.ConnectionString;
                tBL_DONVITableAdapter.Fill(dsDonVi.TBL_DONVI);
                tBL_MucdichTableAdapter.Connection.ConnectionString = DbHelper.ConnectionString;
                tBL_MucdichTableAdapter.Fill(dsMucdich.TBL_Mucdich);
            }
            catch (Exception ex)
            {
                MyBox.ShowWarning(ex.Message, "Tìm kiếm");
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void chxDonvi_CheckedChanged(object sender, EventArgs e)
        {
            txtDonvi.Enabled = chxDonvi.Checked;
        }

        private void chxNgaysudung_CheckedChanged(object sender, EventArgs e)
        {
            dttu.Enabled = chxNgaysudung.Checked;
            dtden.Enabled = chxNgaysudung.Checked;
        }

        private void chxTinhtrang_CheckedChanged(object sender, EventArgs e)
        {
            txtTinhtrang.Enabled = chxTinhtrang.Checked;
        }

        private void btnAction_Click(object sender, EventArgs e)
        {
            Go();
            //if (SqlHelper.LastError
        }

        private void Go()
        {
            var sb = new StringBuilder();
            sb.Append("SELECT TBL_DONVI.MaDonVi, \n");
            sb.Append("       TBL_DONVI.TenDonVi, \n");
            sb.Append("       TBL_THIETBI.MaTB, \n");
            sb.Append("       TBL_THIETBI.TenTB, \n");
            sb.Append("       TBL_THIETBI.NgaySuDung, \n");
            sb.Append("       TBL_Tinhtrang.tinhtrang, \n");
            sb.Append("       TBL_THIETBI.TgianSD, \n");
            sb.Append("       TBL_THIETBI.ThuocTS, \n");
            sb.Append("       a.TenDonVi AS NoiSD, \n");
            sb.Append("       TBL_THIETBI.ID, \n");
            sb.Append("       TBL_nhasx.nhasx, \n");
            sb.Append("       TBL_Mucdich.mucdich, \n");
            sb.Append("       TBL_NHACUNGCAP.TenNCC, \n");
            sb.Append("       TBL_loaiTB.TenLoai, \n");
            sb.Append("       TBL_NHACUNGCAP_1.DiaChi, \n");
            sb.Append("       TBL_THIETBI.NguyenGia, \n");
            sb.Append("       TBL_THIETBI.DiaChiIP, \n");
            sb.Append("       TBL_THIETBI.TgianBH, \n");
            sb.Append("       TBL_THIETBI.GhiChu \n");
            sb.Append("FROM   TBL_THIETBI \n");
            sb.Append("       INNER JOIN TBL_DONVI \n");
            sb.Append("            ON  TBL_THIETBI.MaDonVi = TBL_DONVI.MaDonVi \n");
            sb.Append("       INNER JOIN TBL_Tinhtrang \n");
            sb.Append("            ON  TBL_THIETBI.TinhTrang = TBL_Tinhtrang.id \n");
            sb.Append("       LEFT OUTER JOIN TBL_nhasx \n");
            sb.Append("            ON  TBL_THIETBI.NSX = TBL_nhasx.id \n");
            sb.Append("       LEFT OUTER JOIN TBL_Mucdich \n");
            sb.Append("            ON  TBL_THIETBI.MucDich = TBL_Mucdich.id \n");
            sb.Append("       LEFT OUTER JOIN TBL_NHACUNGCAP \n");
            sb.Append("            ON  TBL_THIETBI.MaNCC = TBL_NHACUNGCAP.MaNCC \n");
            sb.Append("       LEFT OUTER JOIN TBL_loaiTB \n");
            sb.Append("            ON  TBL_THIETBI.MaLoaiTB = TBL_loaiTB.id \n");
            sb.Append("       LEFT OUTER JOIN TBL_NHACUNGCAP AS TBL_NHACUNGCAP_1 \n");
            sb.Append("            ON  TBL_THIETBI.DChiBH = TBL_NHACUNGCAP_1.MaNCC\n");
            sb.Append("       LEFT OUTER JOIN TBL_DONVI a\n");
            sb.Append("            ON  TBL_THIETBI.NoiSD = a.MaDonVi\n");

            sb.Append("WHere MaTB like N'%" + txtMaTB.Text + "%'\n");
            sb.Append("And TenTB like N'%" + txtTenthietbi.Text + "%'\n");
            if (chxDonvi.Checked)
                sb.Append("And TBL_THIETBI.MaDonVi=" + txtDonvi.EditValue + "\n");
            if (chxNgaysudung.Checked)
            {
                sb.Append("And DATEDIFF(day,'" + string.Format("{0:yyyy/MM/dd}", dttu.DateTime) + "',NgaySuDung)>=0 \n");
                sb.Append("And DATEDIFF(day,'" + string.Format("{0:yyyy/MM/dd}", dtden.DateTime) + "',NgaySuDung)<=0 \n");
            }
            if (chxTinhtrang.Checked)
                sb.Append("And TBL_THIETBI.TinhTrang=" + txtTinhtrang.EditValue + "\n");

            if (chxmucdich.Checked)
                sb.Append("And TBL_THIETBI.MucDich=" + txtMucdich.EditValue + "\n");

            sb.Append("ORDER BY TBL_THIETBI.ID");

            dsThietBi.THIETBI.Rows.Clear();
            SqlHelper.Helper.Fill(CommandType.Text, sb.ToString(), dsThietBi.THIETBI);
        }

        private void btnIn_Click(object sender, EventArgs e)
        {
            PrintPreview();
        }

        private void PrintPreview()
        {
            var rpt = new rptThongKeThietBi(dsThietBi.THIETBI);
            // rpt.DataSource = dsThietBi.THIETBI;
            // MessageBox.Show(dsThietBi.THIETBI.Rows.Count.ToString());
            rpt.ShowPreview();
        }

        private void chxmucdich_CheckedChanged(object sender, EventArgs e)
        {
            txtMucdich.Enabled = chxmucdich.Checked;
        }
    }
}